import axios from 'axios';
import qs from 'qs';
import store from '../store/index';
// import { useAxiosCancel } from '@/hooks/useAxiosCancel'
// const CancelToken = axios.CancelToken
// const source = CancelToken.source()

const request = axios.create({
  baseURL: import.meta.env.VITE_BASE_URL,
  // cancelToken: new CancelToken((cancel) => (axiosCancel = cancel)),
});

request.interceptors.request.use(
  (config) => {
    store.commit('auth/changeload',true);
    removePendingRequest(config);
    addPendingRequest(config);
    // const { removePendingRequest, addPendingRequest } = useAxiosCancel(config)
    let token=store.getters['auth/tokken'];
    if(token){
      config.headers['access-token']=token;
    }

    //----------判断get

	
    return config;
  },
  (err) => {
   
    return Promise.reject(err);
  }
);

request.interceptors.response.use(
  (response) => {
    store.commit('auth/changeload',false);
    removePendingRequest(response.config);
    return response;
     
 
  },
  (error) => {
    store.commit('auth/changeload',false);
    removePendingRequest(error.config || {}); // 从pendingRequest对象中移除请求
    
    if(error.response.status==403){
      store.commit('auth/goLogin');
      return Promise.reject('消息过期');
    }

    if (axios.isCancel(error)) {
      // 是取消请求的异常错误
    } else {
      // 其他异常错误
    }
    return Promise.reject(error);
  }
);

// request.post()
// console.dir(request)
export default request;

// 创建请求唯一标识
function generateReqKey(config) {
  const { method, url, params, data } = config;
  //console.log([method, url, qs.stringify(params), qs.stringify(data)].join('&'))
  return [method, url, qs.stringify(params), qs.stringify(data)].join('&');
}
const pendingRequest = new Map(); // 存储请求唯一标识key

// 根据请求标识创建对用的cancelToken函数
function addPendingRequest(config) {
  const requestKey = generateReqKey(config);
  config.cancelToken =
    config.cancelToken ||
    new axios.CancelToken((cancel) => {
      if (!pendingRequest.has(requestKey)) {
        pendingRequest.set(requestKey, cancel);
      }
    });
}
// export let axiosCancel
// 根据请求标识移除对应的cancelToken函数，取消请求
export function removePendingRequest(config) {
  const requestKey = generateReqKey(config);
  if (pendingRequest.has(requestKey)) {
    const axiosCancel = pendingRequest.get(requestKey);
    axiosCancel('取消请求');
    pendingRequest.delete(requestKey);
  }
}

